﻿using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;

namespace Reactor.Workflow.Activities
{
    public class Log : CodeActivity
    {
        public InArgument<Logging.LogLevel> LogLevel { get; set; }
        public InArgument<ILog> Logger { get; set; } 
        public InArgument<Type> LoggerType { get; set; }
        public InArgument<string> Message { get; set; }
        public InArgument<Exception> Exception { get; set; }

        #region Overrides of CodeActivity

        /// <summary>
        /// When implemented in a derived class, performs the execution of the activity.
        /// </summary>
        /// <param name="context">The execution context under which the activity executes.</param>
        protected override void Execute(CodeActivityContext context)
        {
            var logger = Logger.Get(context);
            if (logger == null)
                Logger.Set(context, LogManager.GetLogger(LoggerType.Get(context)));

            var msg = Message.Get(context);
            switch (LogLevel.Get(context))
            {
                case Logging.LogLevel.Debug:
                    logger.Debug(msg);
                    break;
                case Logging.LogLevel.Info:
                    logger.Info(msg);
                    break;
                case Logging.LogLevel.Warn:
                    logger.Warn(msg);
                    break;
                case Logging.LogLevel.Error:
                    logger.Error(msg, Exception.Get(context));
                    break;
                case Logging.LogLevel.Fatal:
                    logger.Fatal(msg, Exception.Get(context));
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        #endregion
    }
}
